//
//  ADView.m
//  Meng
//
//  Created by dllo on 15/9/4.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ADView.h"
#import <UIImageView+WebCache.h>

#define TIME 3



@interface ADView () <UIScrollViewDelegate>

/** 轮播图 */
@property (nonatomic , retain) UIScrollView *scrollView;


/** 轮播图所用的所用图片 */
@property (nonatomic , retain) NSMutableArray *dataArrM;


/** 计时器*/
@property (nonatomic , retain) NSTimer *timer;


/*当前轮播图页数*/
@property (nonatomic , assign) NSInteger page;


@end


@implementation ADView

-(instancetype)initWithFrame:(CGRect)frame dataArr:(NSArray *)dataArr{
    
    self = [super initWithFrame:frame];
    
    if (dataArr.count != 0) {
        if (self) {
            self.backgroundColor = [UIColor whiteColor];
            self.scrollView = [[UIScrollView alloc] init];
            self.scrollView.delegate = self;
            [self addSubview:_scrollView];
            _scrollView.pagingEnabled = YES;
            _scrollView.showsHorizontalScrollIndicator = NO;
            _scrollView.bounces = NO;
            
            self.page = 1;
            
            self.dataArrM = [NSMutableArray array];
            
            [self.dataArrM addObject:[dataArr lastObject]];
            [self.dataArrM addObjectsFromArray:dataArr];
            [self.dataArrM addObject:[dataArr firstObject]];
            _scrollView.contentSize = CGSizeMake(self.frame.size.width*([_dataArrM count]), self.frame.size.height);
            
            self.timer = [NSTimer scheduledTimerWithTimeInterval:TIME target:self selector:@selector(autoScroll) userInfo:nil repeats:true];
            
            NSRunLoop *reunloop = [NSRunLoop currentRunLoop];
            [reunloop addTimer:_timer forMode:NSRunLoopCommonModes];
            
            
        }
    }

    return self;
}

#pragma mark -布局子视图
-(void)layoutSubviews{
    self.scrollView.frame = self.frame;
    
    for (int i = 0; i<_dataArrM.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height - 15 * HEIGHT)];
  
        [imageView sd_setImageWithURL:[NSURL URLWithString:[_dataArrM[i] image_url]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        

        [self.scrollView addSubview:imageView];
    }
    
    
    _scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    
}

#pragma mark - 自动滚动图片
-(void)autoScroll{
    
    self.page++;
    
    [self.scrollView setContentOffset: CGPointMake(self.frame.size.width*self.page, 0) animated:true];
    
    if (self.page == [_dataArrM count]-2) {
        
        [self.scrollView setContentOffset: CGPointMake(self.frame.size.width*self.page+1, 0) animated:true];
        
        /* 图片延迟1.5秒出现 */
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.scrollView setContentOffset: CGPointMake(0, 0) animated:false];
        });
        self.page = 0;
    }
    
   
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    self.page = scrollView.contentOffset.x/self.frame.size.width;
    
    /* 休眠  */
    [self.timer invalidate];
    
    _timer = nil;
    
    
}




#pragma mark - 结束滚动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width*([_dataArrM count]-2), 0);
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width*([_dataArrM count]-1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        
    }
    
    
    self.page = scrollView.contentOffset.x/self.frame.size.width;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:TIME target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    
    /* 在拉动的时候,也可以接着翻页 */
    NSRunLoop *reunloop = [NSRunLoop currentRunLoop];
    
    [reunloop addTimer:_timer forMode:NSRunLoopCommonModes];
    
  
}





@end
